package com.zmn.plat.admin.controller.orderconfig.orderstock.sp;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.exception.ZmnException;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.plat.model.entity.orderconfig.orderstock.SubmitDutyDateVO;
import com.zmn.plat.model.entity.orderconfig.orderstock.sp.SpConfig;
import com.zmn.plat.model.entity.orderconfig.orderstock.sp.SpConfigCloseDayVO;

/**
 * 服务商库存配置
 *
 * @author xiangjunming
 * @since 2020 /04/07 10:43:04
 * @since
 */
@Controller
@RequestMapping("/plat/sp/config/")
public class SpOrderConfigModifyController extends SpOrderConfigBaseController {

    /**
     * logger 描述此字段
     */
    private Logger logger = LoggerFactory.getLogger(SpOrderConfigModifyController.class);

    /**
     * 保存批量设置
     *
     * @param list
     *            the list
     * @return 返回 response dto 描述此返回参数
     * @author xiangjunming
     * @since 2020 /04/07 10:43:04
     * @since
     */
    @RequestMapping(value = "saveBatchSet")
    @ResponseBody
    public ResponseDTO saveBatchSet(@RequestBody List<SpConfig> list) {
        try {
            String realName = getMcStaff().getRealName();
            spConfigBService.saveBatchSet(list, realName);
        } catch (Exception e) {
            logger.error("error:", e);
            return ResponseDTO.fail("ERROR!");
        }
        return ResponseDTO.success();
    }

    /**
     * 关闭库存
     *
     * @param vo
     *            描述此参数
     * @return 返回 response dto 描述此返回参数
     * @author xiangjunming
     * @since 20200324 18:18:25
     * @modify 20200324 xiangjunming 创建
     */
    @RequestMapping(value = "closeDays")
    @ResponseBody
    public ResponseDTO closeDays(@RequestBody SubmitDutyDateVO<SpConfigCloseDayVO> vo) {
        try {
            vo.setOperator(getMcStaff().getRealName());
            spConfigBService.closeDays(vo);
        } catch (ZmnException e) {
            return ResponseDTO.fail(e.getMessage());
        } catch (Exception e) {
            logger.error("error:", e);
            return ResponseDTO.fail("ERROR!");
        }
        return ResponseDTO.success();
    }

    /**
     * 开启库存
     *
     * @param ids
     *            the ids
     * @return 返回 response dto 描述此返回参数
     * @author xiangjunming
     * @since 20200324 18:18:25
     * @modify 20200324 xiangjunming 创建
     */
    @RequestMapping(value = "openDays")
    @ResponseBody
    public ResponseDTO openDays(@RequestBody List<Integer> ids) {
        try {
            spConfigBService.openDays(ids, getMcStaff().getRealName());
        } catch (ZmnException e) {
            return ResponseDTO.fail(e.getMessage());
        } catch (Exception e) {
            logger.error("error:", e);
            return ResponseDTO.fail("ERROR!");
        }
        return ResponseDTO.success();
    }

    /**
     * 修改节假日系数
     *
     * @param configId
     *            the config id
     * @param value
     *            the value
     * @return the result dto
     */
    @RequestMapping(value = "updateCoefficient")
    @ResponseBody
    public ResponseDTO updateCoefficient(Integer configId, Double value) {
        try {
            SpConfig spConfig = new SpConfig();
            spConfig.setConfigId(configId);
            spConfig.setCoefficient(value);
            spConfig.setUpdater(getMcStaffRealName());
            spConfig.setUpdateTime(DateUtil.getNow());
            spConfigService.updateByKey(spConfig);
        } catch (Exception e) {
            logger.error("error:", e);
            return ResponseDTO.fail("ERROR!");
        }
        return ResponseDTO.success();
    }

    /**
     * 修改库存量
     *
     * @param configId
     *            the config id
     * @param stock
     *            the value
     * @return the result dto
     */
    @RequestMapping(value = "updateStock")
    @ResponseBody
    public ResponseDTO updateStock(Integer configId, Integer stock) {
        try {
            if (stock == null) {
                stock = -1;
            }
            SpConfig spConfig = new SpConfig();
            spConfig.setConfigId(configId);
            spConfig.setStock(stock);
            spConfig.setUpdater(getMcStaffRealName());
            spConfig.setUpdateTime(DateUtil.getNow());
            spConfigService.updateByKey(spConfig);
        } catch (Exception e) {
            logger.error("error:", e);
            return ResponseDTO.fail("ERROR!");
        }
        return ResponseDTO.success();
    }
}
